<?php


namespace App\Http\service;

use App\Models\Sys;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Log;

class GzhService
{
    private $app;
    public function __construct()
    {
        $config=[
            'app_id' => Sys::getValue("gzh_appid"),
            'secret' => Sys::getValue("gzh_secret"),

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',
        ];
        $this->app=Factory::officialAccount($config);
        $accessToken = $this->app->access_token;
        $token = $accessToken->getToken();
        if (!$token){
            $token = $accessToken->getToken(true);
        }
    }

    public function send_message($data)
    {
        $res=$this->app->template_message->send([
            'touser' => $data["openid"],
            'template_id' => 'jL-Exz_r7hL0zn9PyGvxSflaDWDOT3mDHsgyY3htFrw',
            'url' => 'https://easywechat.org',
            'miniprogram' => [
                'appid' =>Sys::getValue("wx_appid"),
                'pagepath' => 'pages/index/index',
            ],
            'data' => [
                'first' => '消息回复',
                'keyword1' => date("Y-m-d H:i:s"),
                'keyword2' =>$data["name"],
                'keyword3' => $data["message"],
                "remark"=>"请及时查看"
            ],
        ]);
    }

    public function invite_notice($data)
    {
        
        $res=$this->app->template_message->send([
            'touser' => $data["openid"],
            'template_id' => 'YZlUDQh4PWJeJiHos-QCnHpKDoSwvg3-d1hqd1ciQF4',
            'url' => 'https://easywechat.org',
            'miniprogram' => [
                'appid' =>Sys::getValue("wx_appid"),
                'pagepath' => 'pages/index/index?type=share_coupon',
            ],
            'data' => [
                'first' => '恭喜您成功领取抵扣卷',
                'keyword1' =>$data["price"],
               // 'keyword2' =>date("Y-m-d H:i:s"),
                "remark"=>"请及时查看"
            ],
        ]);
      /*  $res=$this->app->template_message->send([
            'touser' => $data["openid"],
            'template_id' => 'qsocYe3xFws3tqvFdx7OhFmpT8X-FYlTFGQwbruAPQw',
            'url' => 'https://easywechat.org',
            'miniprogram' => [
                'appid' =>Sys::getValue("wx_appid"),
                'pagepath' => 'pages/index/index?type=share_coupon',
            ],
            'data' => [
                'first' => '邀请成功',
                'keyword1' =>$data["nickname"],
                'keyword2' =>date("Y-m-d H:i:s"),
                "remark"=>"请及时查看"
            ],
        ]);*/
    }

    public function getUser($nextOpenId = null)
    {
        return $this->app->user->list($nextOpenId);
    }

    public function getOneUser($openid)
    {
        return $this->app->user->get($openid);
    }

    public function getUsers($openids)
    {
        return $this->app->user->select($openids);
    }

}
